var express = require("express");
var router = express.Router();
var router = express.Router();
var query = require('./mysql')


// 登录请求
router.post('/pa', async (req, res) => {
    let data = req.body.data;
    let rows = await query("select * from login where phone=? && pass=? ", [data.name, data.passworld]);
    console.log(rows)
    let json = {};
    if (rows.length != 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = {
            code: 100
        }
    }
    res.send(json)
});
// 注册请求
router.post('/newUser', async (req, res) => {
    let data = req.body.data;
    console.log(data);
    console.log("+++++++++++")

    let list = await query("select * from login where phone=? ", [data.phone]);

    let json = {};
    if (list.length == 0) {
        let rows = await query('insert into login(phone,pass) values("' + data.phone + '","' + data.passworld + '")');

        json = {
            code: 200,
            result: data,
        }
    } else {
        json = {
            code: 100
        }

        res.send(json)
    }



});



// 找回密码
router.post('/back', async (req, res) => {
    let data = req.body.data;
    console.log(data);
    console.log("+++++++++++")
    let list = await query("select * from login where phone=? ", [data.phone]);
    
    let json = {};
    if (list.length != 0) {
        let rows = await query("update login set pass="+data.passworld+" where phone="+data.phone);
        json = {
            code: 200,
            result: data,
        }
    } else {
        json = {
            code: 100
        }
    }
    res.send(json)



});
module.exports = router;